/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2018
 */

package com.bm.console.action;

import java.util.List;

import com.bm.console.business.WebCityService;
import com.bm.console.business.WebTendencyService;
import com.bm.console.pojo.WebCityItem;
import com.bm.console.pojo.WebTendency;
import org.springframework.web.bind.annotation.RequestBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bm.console.business.WebHelpFindHouseService;
import com.bm.console.pojo.WebHelpFindHouse;
import com.sys.object.common.ObjException;
import com.sys.object.common.ObjResultCode;
import com.sys.object.common.ObjResultException;
import com.sys.object.common.ObjResultJSON;
import com.sys.object.common.page.PageBean;
@Controller
public class WebHelpFindHouseAction{
	@Resource
	WebTendencyService webTendencyService;

	@Resource WebHelpFindHouseService webHelpFindHouseService;

	@Resource
	WebCityService webCityService;

	@RequestMapping(value = "/webHelpFindHouseList.do")
	public String webHelpFindHouseList(PageBean pageBean, Model model, WebHelpFindHouse webHelpFindHouse) {
		try {
			pageBean = webHelpFindHouseService.queryPageList(webHelpFindHouse, pageBean);
			model.addAttribute("pageBean", pageBean);
			model.addAttribute("startRow", pageBean.getStartRow());
			model.addAttribute("entity", webHelpFindHouse);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/web/webhelpfindhouse/list";
	}

	/**
	 * 新增动态 Get
	 */
	@RequestMapping(value = "/addWebHelpFindHouse.do", method = RequestMethod.GET)
	public String addWebHelpFindHouse(@ModelAttribute("webHelpFindHouse") WebHelpFindHouse webHelpFindHouse, Model model, String id) {
		Integer prow = 0;
		try {
			// ArrayList<DictCategory> listcate = bbsService.selectByAll();
			if (StringUtils.hasText(id)) {
				WebHelpFindHouse record = webHelpFindHouseService.queryByPrimaryKey(Integer.parseInt(id));
				model.addAttribute("entity", record);
			}
			List<WebCityItem> city_list = webCityService.selectAll();
			model.addAttribute("city_list", city_list);

			WebTendency webTendency = new WebTendency();
			webTendency.setStatus(true);
			webTendency.setPid(1);
			List<WebTendency> investment_objective_list = webTendencyService.selectAll(webTendency);
			model.addAttribute("investment_objective_list", investment_objective_list);

			webTendency.setPid(2);
			List<WebTendency> capital_source_list = webTendencyService.selectAll(webTendency);
			model.addAttribute("capital_source_list", capital_source_list);

			webTendency.setPid(3);
			List<WebTendency> tendency_room_list = webTendencyService.selectAll(webTendency);
			model.addAttribute("tendency_room_list", tendency_room_list);

			webTendency.setPid(4);
			List<WebTendency> tendency_price_list = webTendencyService.selectAll(webTendency);
			model.addAttribute("tendency_price_list", tendency_price_list);

			webTendency.setPid(5);
			List<WebTendency> tendency_size_list = webTendencyService.selectAll(webTendency);
			model.addAttribute("tendency_size_list", tendency_size_list);

			webTendency.setPid(6);
			List<WebTendency> tendency_school_list = webTendencyService.selectAll(webTendency);
			model.addAttribute("tendency_school_list", tendency_school_list);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("prow", prow);
		return "/web/webhelpfindhouse/add";
	}

	/**
	 * 新增动态 Post
	 */
	@RequestMapping(value = "/saveWebHelpFindHouse.do", method = RequestMethod.POST)
	public String saveWebHelpFindHouse(@Validated @ModelAttribute("webHelpFindHouse") WebHelpFindHouse webHelpFindHouse,
			BindingResult result, Model model, Integer prow,
			HttpServletRequest request) {
		ObjResultException<String> ret = new ObjResultException<String>();
		try {
			int success = webHelpFindHouseService.saveOrUpdate(webHelpFindHouse);
			if (success > 0) {
				ret.setResultCode(ObjResultCode.OK);
			} else {
				ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
			}
		} catch (Exception e) {

			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.ERROR);

			}
			e.printStackTrace();
			model.addAttribute("entity", webHelpFindHouse);
			model.addAttribute("ret", ret);
			return "/web/webhelpfindhouse/add";
		}
		return "redirect:/webHelpFindHouseList.do?status=";
	}
	
	/**
	 * 新增动态 Post
	 */
	@RequestMapping(value = "/saveWebHelpFindHouseJson.do", method = RequestMethod.POST)
	@ResponseBody
	public ObjResultException<String> saveWebHelpFindHouseJson(@RequestBody WebHelpFindHouse webHelpFindHouse, Model model,
			HttpServletRequest request) {
		ObjResultException<String> ret = new ObjResultException<String>();
		try {
			int success = webHelpFindHouseService.saveOrUpdate(webHelpFindHouse);
			if (success > 0) {
				ret.setResultCode(ObjResultCode.OK);
			} else {
				ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
			}
		} catch (Exception e) {
			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.ERROR);
			}
		}
		return ret;
	}

	/**
	 * 删除APP
	 */
	@RequestMapping("/deleteWebHelpFindHouse.do")
	@ResponseBody
	public ObjResultException<String> deleteWebHelpFindHouse(Integer id) {
		ObjResultException<String> ret = new ObjResultException<String>();
		try {
			int success = webHelpFindHouseService.delete(id);

			if (success > 0) {
				ret.setResultCode(ObjResultCode.OK);
			} else {
				ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
			}
		} catch (Exception e) {
			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.ERROR);
			}
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * 修改状态APP
	 */
	@RequestMapping("/updateStatusWebHelpFindHouse.do")
	@ResponseBody
	public ObjResultException<String> updateStatusWebHelpFindHouse(WebHelpFindHouse webHelpFindHouse) {
		ObjResultException<String> ret = new ObjResultException<String>();
		try {
			int success = webHelpFindHouseService.updateStatus(webHelpFindHouse);

			if (success > 0) {
				ret.setResultCode(ObjResultCode.OK);
			} else {
				ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
			}
		} catch (Exception e) {
			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.ERROR);
			}
		}
		return ret;
	}
}